//
//  EventThumbViem.m
//  lvtu
//
//  Created by Apple on 12-7-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "EventThumbViem.h"
#import "EGOImageView.h"
#import "EventDetailViewController.h"
#import "UIView+Sizes.h"
#import "wxAppDelegate.h"

@implementation EventThumbViem
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame Data:(PlaceInfo*)info
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSString* imgurl;
        NSString* name;
        if ([info isKindOfClass:[NSDictionary class]]) {
            NSDictionary* list = (NSDictionary*)info;
            imgurl = [list objectForKey:@"image"];
            name = [list objectForKey:@"name"];
            _info = [[PlaceInfo alloc] init];
            _info.shopid = [list objectForKey:@"c_shop_id"];
            _info.name = [list objectForKey:@"name"];
            _info.imageurl = [list objectForKey:@"image"];
            _info.typeid = [list objectForKey:@"typeid"];
        }
        else {
            imgurl = info.imageurl;
            name = info.name;
            _info = info;
        }
        EGOImageView* ImageView = [[EGOImageView alloc] initWithFrame:
                                  CGRectMake(0, 0, self.frame.size.width, 70)];
        [ImageView setImageURL:[NSURL URLWithString:imgurl]];
        [self addSubview:ImageView];
        [ImageView release];
        
        UIView* shadowView = [[UIView alloc] initWithFrame:CGRectMake(24, self.frame.size.height - 20, self.frame.size.width-24, 20)];
        shadowView.backgroundColor = [UIColor colorWithRed:30/255.0 green:30/255.0 blue:30/255.0 alpha:0.4];
//        [self addSubview:shadowView];
        [shadowView release];
        
        UIImage* delImage = [UIImage imageNamed:@"btn_delete_normal.png"];
        delButton = [UIButton buttonWithType:UIButtonTypeCustom];
        delButton.frame = CGRectMake(self.frame.size.width - 25, -3, 30, 30);
        [delButton setBackgroundImage:delImage forState:UIControlStateNormal];
        [delButton setBackgroundImage:[UIImage imageNamed:@"btn_delete_press.png"] forState:UIControlStateHighlighted];
        [delButton addTarget:self action:@selector(delButtonPress:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:delButton];
        
        UIImage *icnImage;
        if ([info.typeid isEqualToString:@"1"]) {
            icnImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icn_chi.png" ofType:nil]];
        }
        else if ([info.typeid isEqualToString:@"2"])
        {
            icnImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icn_zhu.png" ofType:nil]];
        }
        else if ([info.typeid isEqualToString:@"4"])
        {
            icnImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icn_you.png" ofType:nil]];
        }
        else if ([info.typeid isEqualToString:@"5"])
        {
            icnImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icn_gou.png" ofType:nil]];
        }
        else if ([info.typeid isEqualToString:@"6"])
        {
            icnImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icn_yu.png" ofType:nil]];
        }
        else
        {
            icnImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icn_yu.png" ofType:nil]];
        }
        UIImageView* icnView = [[UIImageView alloc] initWithImage:icnImage];
        [icnImage release];
        [icnView setFrame:CGRectMake(0, self.frame.size.height - 16, 12, 12)];
        [self addSubview:icnView];
        
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(14, self.frame.size.height - 15, self.frame.size.width-14, 12)];
        if ([name rangeOfString:@"("].length > 0) {
            label.text = [name substringToIndex:[name rangeOfString:@"("].location];
        }
        else {
            label.text = name;
        }
        label.textColor = [UIColor grayColor];
        label.font = [UIFont systemFontOfSize:11];
        label.textAlignment = UITextAlignmentLeft;
        label.backgroundColor = [UIColor clearColor];
        [self addSubview:label];
        [label release];
    }
    return self;
}

-(void)dealloc
{
    self.delegate = nil;
    [super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (void)setViewType:(BOOL)canEdit
{
    if (canEdit) {
        [delButton setHidden:NO];
    }
    else
    {
        [delButton setHidden:YES];
    }
}

- (void)delButtonPress:(id)sender
{
    [delegate delButtonPressed:_info];
}

#pragma mark - touch
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self];
    if (location.x > self.frame.size.width || location.x < 0 || location.y > self.frame.size.height || location.y < 0) {
        return;
    }
    if ([wxAppDelegate getAppDelegate].isLogin == NO) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"需要登录才能看到具体事件哟。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        [alert release];
        return;
    }
    EventDetailViewController * eventDetailViewController = [[EventDetailViewController alloc] initWithPlaceInfo:_info];
    
    NSArray* currentArray = [[DataSource shareInstance] getCurrentEvent];
    for (PlaceInfo* place in currentArray) {
        if ([_info.shopid isEqualToString:place.shopid]) {
            eventDetailViewController.isTripExistEvent = YES;
        }
    }
    if (delButton.hidden == YES) {
        eventDetailViewController.isEdit = NO;
    }
    else
    {
        eventDetailViewController.delegate = self.viewController;
    }
    
    [self.viewController.navigationController pushViewController:eventDetailViewController animated:YES];
    [eventDetailViewController release];
    [super touchesEnded:touches withEvent:event];
}

#pragma mark - button
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [[wxAppDelegate getAppDelegate] popAllViewController];
        [[wxAppDelegate getAppDelegate] launchFirstView];
    }
}

@end
